'use strict';

var mongoose = require('mongoose');
var Product = require('./productModel');
var Company = require('./companyModel');

var sysinfoModel = function () {
    var sysinfoSchema = mongoose.Schema({
        usersCount: Number,
        productCount: Number,
        companyCount: Number,
        customHits: Number
    });

    //Verbose toString method
    sysinfoSchema.methods.getProductCount = function () {
        Product.find(function (err, prods) {
            if (err) {
                console.log(err);
            }
            prods.forEach(function(prod) {
                prod.prettyPrice = prod.prettyPrice();
            });
            var model =
                    {
                        products: prods
                    };
            res.render('products', model);                  // 'products' 是dust的模块文件
        });
    };

    //Format the price of the product to show a dollar sign, and two decimal places
    sysinfoSchema.methods.prettyPrice = function () {
        return (this && this.price) ? '$' + this.price.toFixed(2) : '$';
    };

    return mongoose.model('Sysinfo', sysinfoSchema);

};

module.exports = new sysinfoModel();
